import { createStore } from 'vuex'
import persistedstate from 'vuex-persistedstate';
export default createStore({
  state: {
    cartList:[], // 购物车的数据
  },
  mutations: {
    addCart(state,val){ // 加入购物车数据，判断商品id是否相同
      let index = state.cartList.findIndex(item => item.product_id == val.product_id);
      if (index>-1){
        state.cartList[index].buyNum += val.buyNum
      } else {
        state.cartList.push(val);
      }
      
    }
  },
  actions: {
  },
  modules: {
  },
  plugins:[
    new persistedstate({
      storage: window.localStorage,
    }),
  ]
})
